package com.zxx.cascommon.base.result;

import java.util.ArrayList;
import java.util.List;

/**
 * 返回code枚举类 code2000以下为基础返回码 code2000以上为业务返回码
 *
 * @author zhoukai
 * @date 2020/3/12 上午9:38
 */
public enum ResultEnum {
    //**
    FAIL(-1, "操作失败"),
    SUCCESS(1000, "操作成功"),
    PARAMS_ERROR(1100, "参数错误"),
    TOKEN_ERROR(1101, "token错误"),
    TOKEN_EXPIRED(1102, "token过期"),
    REQUEST_RATE_LIMIT(1103, "接口流量控制"),
    INTERNAL_ERROR(1200, "内部服务异常"),
    DATA_NO_CHANGE(1201, "数据没有变化"),
    DATA_NOT_EXIST(1202, "数据不存在"),
    DATA_ALREADY_EXIST(1203, "数据已经存在"),
    PASSWORD_ERROR(1204, "密码错误"),
    ;

    private Integer code;
    private String name;

    ResultEnum(Integer code, String name) {
        this.code = code;
        this.name = name;
    }

    public static List<String> getAllName() {
        List<String> names = new ArrayList<>(16);
        for (ResultEnum c : ResultEnum.values()) {
            names.add(c.getName());
        }
        return names;
    }

    public static List<Integer> getAllCode() {
        List<Integer> codes = new ArrayList<>(16);
        for (ResultEnum c : ResultEnum.values()) {
            codes.add(c.getCode());
        }
        return codes;
    }

    public static String getName(Integer code) {
        for (ResultEnum c : ResultEnum.values()) {
            if (c.getCode().equals(code)) {
                return c.name;
            }
        }
        return null;
    }

    public static String getCode(String name) {
        for (ResultEnum c : ResultEnum.values()) {
            if (c.getName().equals(name)) {
                return c.name;
            }
        }
        return null;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}
